<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\tree_table\util\ttroperation;

/**
 * AJAX
 * @author cleverstone
 * @since ym2.0
 */
class Ajax extends OperationType
{

    const TYPE = 'ajax';

    /**
     * @var string 分组
     */
    private $_group = 'normal';

    /**
     * @var array 选项
     */
    private $_options = [
        'title' => '',
        'icon' => '',
        'route' => null,
        'params' => [], // 请求参数, 默认主键
        'method' => 'GET',
        'questionTitle' => '', // 询问标题
        'questionDescription' => '确定要执行该操作么?', // 询问描述
        'operationId' => null,
        '__isRender' => true,
    ];

    /**
     * 是否添加分组
     * @return $this
     */
    public function group()
    {
        $this->_group = 'more';
        return $this;
    }

    /**
     * @param string $id 操作项ID
     * @return $this
     */
    public function id($id)
    {
        $this->_options['operationId'] = $id;
        return $this;
    }

    /**
     * @param string $title 名称标题
     * ```php
     * // 文本标题
     * $this->title('编辑');
     *
     * // 支持HTML
     * $this->title(H5::text('编辑'));
     *
     * ```
     * @return $this
     */
    public function title($title)
    {
        $this->_options['title'] = $title;
        return $this;
    }

    /**
     * @param string $questionTitle 询问标题
     * @return $this
     */
    public function questionTitle($questionTitle)
    {
        $this->_options['questionTitle'] = $questionTitle;
        return $this;
    }

    /**
     * @param string $questionDescription 询问描述
     * @return $this
     */
    public function questionDescription($questionDescription)
    {
        $this->_options['questionDescription'] = $questionDescription;
        return $this;
    }

    /**
     * @param string $icon 操作项按钮图标
     * 支持的图标有:
     * - bootstrap v4图标
     * - fontawesome v5
     * - layui 2.8图标
     *
     * @return $this
     */
    public function icon($icon)
    {
        $this->_options['icon'] = $icon;
        return $this;
    }

    /**
     * @param string $route 路由或外链
     * ```php
     * 可以直接指定路由
     * $this->route('user/edit');
     *
     * 也可以指定外部链接
     * $this->route('https://ym2.cleverstone.top');
     *
     * ```
     * @return $this
     */
    public function route($route)
    {
        $this->_options['route'] = $route;
        return $this;
    }

    /**
     * @param array|string $params 请求参数
     * one:
     * ```php
     * // 直接指定query中的字段
     * $this->params(['id']);
     *
     * ```
     *
     * two:
     * ```php
     * // key-value, 字段指定值
     * $this->params(['id' => 1]);
     *
     * ```
     *
     * three:
     * ```php
     * // key-占位符, 字段+字段占位符
     * $this->params(['action' => ':status']);
     *
     * ```
     * @return $this
     */
    public function params($params)
    {
        $this->_options['params'] = (array)$params;
        return $this;
    }

    /**
     * @param string $method HTTP请求动作, 目前支持GET(助手默认), POST
     * @return $this
     */
    public function method($method = 'POST')
    {
        $this->_options['method'] = strtoupper($method);
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        $route = $this->_options['route'];
        if (!empty($route)) {
            // rbac校验失败
            if (!$this->checkAccess($route)) {
                $this->_options['__isRender'] = false;
            }
        }

        $configure = [];
        $configure['type'] = self::TYPE;
        $configure['group'] = $this->_group;
        $configure['options'] = $this->_options;

        return $configure;
    }
}